﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.Data.Week
{
    /// <summary>
    /// 公司周累计
    /// </summary>
    public class CompanyWeekSum
    {
        public int Id { get; set; }

        /// <summary>
        /// 交易日
        /// </summary>
        public int Year { get; set; }

        public int Month { get; set; }

        public int Week { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        public void InitCompanyWeekSum(int year, int week, int month, DB.DBManager db)
        {
            CompanyWeekSum sum = new CompanyWeekSum();
            var query = db.OperatorWeekSum.Where(a => a.Year == year).Where(a => a.Week == week);
            var queryExist = db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week);
            if (queryExist.Count() > 0)
            {
                queryExist.First().AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                queryExist.First().AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                queryExist.First().BillCount = query.Sum(a => a.BillCount);
                queryExist.First().DayCount = query.Sum(a => a.DayCount);
                queryExist.First().Free = query.Sum(a => a.Free);
                queryExist.First().LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                queryExist.First().MaxReturn = query.Sum(a => a.MaxReturn);
                queryExist.First().OverStopLoss = query.Sum(a => a.OverStopLoss);
                queryExist.First().PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                queryExist.First().Profit = query.Sum(a => a.Profit);
                queryExist.First().RaiseRate = query.Sum(a => a.RaiseRate);
                queryExist.First().RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                queryExist.First().SumLost = query.Sum(a => a.SumLost);
                queryExist.First().SumWin = query.Sum(a => a.SumWin);
                queryExist.First().UsedMargin = query.Sum(a => a.UsedMargin);
                queryExist.First().Utilization = query.Sum(a => a.Utilization);
                queryExist.First().WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                queryExist.First().WinRate = query.Sum(a => a.WinRate);
                queryExist.First().Yield = query.Sum(a => a.Yield);
            }
            else
            {
                sum.AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                sum.AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                sum.BillCount = query.Sum(a => a.BillCount);
                sum.DayCount = query.Sum(a => a.DayCount);
                sum.Free = query.Sum(a => a.Free);
                sum.LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                sum.MaxReturn = query.Sum(a => a.MaxReturn);
                sum.OverStopLoss = query.Sum(a => a.OverStopLoss);
                sum.PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                sum.Profit = query.Sum(a => a.Profit);
                sum.RaiseRate = query.Sum(a => a.RaiseRate);
                sum.RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                sum.SumLost = query.Sum(a => a.SumLost);
                sum.SumWin = query.Sum(a => a.SumWin);
                sum.UsedMargin = query.Sum(a => a.UsedMargin);
                sum.Utilization = query.Sum(a => a.Utilization);
                sum.WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                sum.WinRate = query.Sum(a => a.WinRate);
                sum.Yield = query.Sum(a => a.Yield);
                sum.Year = year;
                sum.Week = week;
                sum.Month = month;

                db.CompanyWeekSum.Add(sum);
            }

            db.SaveChanges();
        }

        public void Delete(int year, int week, DB.DBManager db)
        {
            var query = db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week).ToList();
            foreach (BenefitHelper.Data.Week.CompanyWeekSum avg in query)
            {
                db.CompanyWeekSum.Remove(avg);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public CompanyWeekSum GetCompanyWeekSum(int year, int week, DB.DBManager db)
        {
            return db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week).ToList().First();
        }

        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<CompanyWeekSum> GetCompanyWeekSums(int year, int month, DB.DBManager db)
        {
            return db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Month == month).ToList();
        }
    }
}
